package vip.xiaonuo.listener.impl;

import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.device.entity.Device;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.device.service.DeviceService;
import vip.xiaonuo.facedoor.modular.device.service.DeviceSync;
import vip.xiaonuo.megvii.service.DeviceMegviiBaseService;
import vip.xiaonuo.modular.devicemegvii.entity.DeviceMegvii;
import vip.xiaonuo.modular.devicemegvii.service.DeviceMegviiService;

import javax.annotation.Resource;

@Service("AiBoxDeviceSyncService.class")
public class AiBoxDeviceSyncService implements DeviceSync<DeviceMegvii> {

    @Resource
    private DeviceMegviiService megviiService;

    @Resource
    private DeviceService deviceService;

    @Override
    public Long add(Object obj) {
        try{
            DeviceMegvii megvii = (DeviceMegvii)obj;
            Device device = new Device();
            this.createDevice(device,megvii);
            deviceService.saveOrUpdate(device);
            return device.getId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0L;
    }

    @Override
    public Long update(Object obj) {
        try{
            DeviceMegvii megvii = (DeviceMegvii)obj;
            if(megvii==null){
                return 0L;
            }

            if( megvii.getDeviceId()==null){
                return this.add(obj);
            }else{
                Device device = deviceService.getById(megvii.getDeviceId());
                if(device==null){
                    return this.add(obj);
                }else{
                    this.createDevice(device, megvii);
                    deviceService.saveOrUpdate(device);
                    return 0L;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0L;
    }

    @Override
    public void delete(Object obj) {
        DeviceMegvii megvii = (DeviceMegvii)obj;
        if(megvii!=null && megvii.getDeviceId()!=null){
            deviceService.removeById(megvii.getDeviceId());
        }
    }

    @Override
    public void saveRelate(Long id, DeviceMegvii deviceMegvii) {
        if(id!=null && id>0){
            deviceMegvii.setDeviceId(id);
            megviiService.saveOrUpdate(deviceMegvii);
        }
    }

    private void createDevice(Device device,DeviceMegvii megvii){
        device.setDeviceNo(megvii.getMachNo());
        device.setSnCode(megvii.getSnCode());
        device.setName(megvii.getMachName());
        device.setType(DeviceCategory.MAIN_TYPE_BOX);
        device.setSubType(DeviceCategory.SUB_TYPE_BOX);
        device.setInsLocation(megvii.getMachLocation());
        device.setNetAddress(DeviceMegviiBaseService.getMachAddress(megvii));
        device.setStatus(convertStatus(megvii.getMachStatus()));
    }

    /**
     *
     * @param status 旷视设备的状态
     * @return
     */
    private int convertStatus (int status){
        // 0-关机 1-在线 2-断线
        // {code:0,name:"未验证"},{code:1,name:"终端地址错误"},{code:2,name:"账号密码错误"},{code:3,name:"连接成功"}
        switch (status){
            case 0 :
                return 0;
            case 1 :
            case 2 :
                return 2;
            case 3 :
                return 1;
        }
        return 0;
    }
}
